package team.seekers.hula.question.question.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.VisibilityUpdateDTO;
import team.seekers.hula.question.question.entity.dto.QuestionAddDTO;
import team.seekers.hula.question.question.entity.dto.QuestionQueryDTO;
import team.seekers.hula.question.question.entity.dto.QuestionUpdateDTO;
import team.seekers.hula.question.question.entity.vo.QuestionHotListVO;
import team.seekers.hula.question.question.entity.vo.QuestionInfoVO;
import team.seekers.hula.question.question.entity.vo.QuestionListVO;
import team.seekers.hula.question.question.service.QuestionService;
import team.seekers.hula.question.questionUser.service.QuestionUserService;

import java.util.List;

/**
 * 问题前端控制器
 * @author hbc
 * @since 2021-02-15
 */
@Controller
@RequestMapping("/question")
@ApiResponseBody
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionUserService questionUserService;

    @PostMapping("/add")
    public void add(@RequestBody QuestionAddDTO addDTO) {
        addDTO.setUserId(MindUtils.getUserId());
        questionService.add(addDTO);
    }

    @GetMapping("/page")
    public PageResult<QuestionListVO> page(QuestionQueryDTO questionQuery){
        return questionService.pageOrderByHeat(questionQuery);
    }


    @GetMapping("/delete")
    public void delete(@RequestParam("QuestionId") String id) {
        questionService.delete(id);
    }


    @PostMapping("/update/info")
    public void updateInfo(@RequestBody QuestionUpdateDTO updateVO) {
        questionService.updateInfo(updateVO);
    }

    @PostMapping("/update/visibility")
    public void updateVisibility(@RequestBody VisibilityUpdateDTO updateDTO) {
        questionService.updateVisibility(updateDTO);
    }


    @GetMapping("/info")
    public QuestionInfoVO get(@RequestParam String id) {
        return questionService.getInfoVO(id);
    }



    //获取热点问题列表
    @GetMapping("/list-hot")
    public List<QuestionHotListVO> listHot() {
        return questionService.listHot();
    }

    //分类型分页查询按照热度排序（热榜）
    @PostMapping("/pageOrderByHeat")
    public PageResult<QuestionListVO> pageOrderByHeat(@RequestBody QuestionQueryDTO questionQuery) {
        return questionService.pageOrderByHeat(questionQuery);
    }

    // 从关注变为不关注，或者从不关注变为关注
    @GetMapping("/user/change")
    public void changeFollow(@RequestParam("id") String questionId){
        questionUserService.changeFollow(questionId);
    }


}
